package cn.itcast.web.action;

import cn.itcast.domain.constant.Constants;
import cn.itcast.domain.delivery.Promotion;
import cn.itcast.domain.page.PageBean;
import cn.itcast.utils.ResponseUtils;
import cn.itcast.web.action.common.BaseAction;
import com.alibaba.fastjson.JSONObject;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.io.FileUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.ws.rs.core.MediaType;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

@Namespace("/")
@ParentPackage("struts-default")
@Controller
@Scope("prototype")
public class PromotionBofAction extends BaseAction<Promotion> {
    private Promotion promotion = new Promotion();

    @Action(value = "promotion_pageQuery")
    public String pageQuery() {
        PageBean<Promotion> pageBean = WebClient.create(Constants.BOS_HOST+ Constants.BOS_URL+"/services/promotionService/pagequery/" + page + "/" + rows)
                .accept(MediaType.APPLICATION_JSON).get(PageBean.class);
        String json =JSONObject.toJSONString(pageBean);
        ResponseUtils.responseWriter(json);
        return NONE;
    }
  //return "promotion_showDetails.action?id="+$routeParams.id;
    @Action(value = "promotion_showDetails")
    public String showDetails() throws IOException, TemplateException {
        //创建以id为名的html页面
        String realPath = ServletActionContext.getServletContext().getRealPath("/freemarker");
        File htmlFile=new File(realPath,model.getId()+".html");
        //文件不存在,则动态生成html
        if(!htmlFile.exists()){
            //获取configuration对象 设置模板版本
            Configuration configuration =new Configuration(Configuration.VERSION_2_3_22);
            configuration.setDefaultEncoding("utf-8");
            //设置模板路径
            configuration.setDirectoryForTemplateLoading(new File(ServletActionContext.getServletContext().getRealPath("/WEB-INF/freemarker_templates")));
            //获取模板对象
            Template template = configuration.getTemplate("promotion_detail.ftl");
            //获取动态数据
            Promotion promotion = WebClient.create(Constants.BOS_HOST+ Constants.BOS_URL+"/bos/services/promotionService/promotion/" + model.getId())
                    .accept(MediaType.APPLICATION_JSON).get(Promotion.class);
            //封装数据
            Map<String,Object>parameterMap=new HashMap<>();
            parameterMap.put("promotion",promotion);
            //合并输出  param 1数据 2输出流
            template.process(parameterMap,new OutputStreamWriter(new FileOutputStream(htmlFile),"utf-8"));

        }
        //文件存在,则直接响应文件的输出流
        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        try {
            FileUtils.copyFile(htmlFile,ServletActionContext.getResponse().getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return NONE;
    }

}